<?php
/*
 *      auth.class.php
 *      
 *      Copyright 2009 Alberto Berroteran <ashrey10@yahoo.es>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

class Auth{
	protected $s_username;
	protected $s_password;
	protected $i_userid;
	protected $s_ip;
	protected $i_stime;
	protected $i_sinit;
	protected $b_islogin;
	public $s_lasterror = 'No error';
	
	static $s_name = 'auth-web';

	public static function  getInstance(){
		if(isset($_SESSION[self::$s_name])){
			return unserialize($_SESSION[self::$s_name]);
		}else{
			return new Auth();
		}
	}
	
	private function __construct(){
		$this->i_sinit = time();
		$_SESSION[self::$s_name] = serialize($this);
	}
	
	function setTime(){
		$this->i_stime = $i_stime;
	}
	
	function isLogin(){
		if($this->i_stime > (time()-$this->i_sinit)){
			return false;
		}else{
			return $this->b_islogin;
		}
	}
	
	function login($s_user, $s_pass){
		$_CONF = ABConf::getAll();
		$this->s_username = $s_user;
		$this->s_password = sha1($s_pass);
		$this->s_ip=$_SERVER['REMOTE_ADDR'];
		
		if($_CONF['pass']== $this->s_password and $_CONF['name'] == $this->s_username){
			$this->b_islogin = true;
			$this->s_username = $_CONF['name'];
			return true;
		}else{
			$this->s_lasterror = 'Datos de autentificación incorrecto';
			return false;
		}
	}
	
	function getUsername(){
		return $this->s_username;
	}
	
	function getUserId(){
		return $this->i_userid;
	}

	/**
	*End the session
	*/
	function destroy(){
		unset($_SESSION[self::$s_name]);
	}
}
?>
